local assets = { Asset("ANIM", "anim/obrsword.zip"),                                                       -- 地上的动画
    Asset("ANIM", "anim/swap_obrsword.zip"),                                                               -- 手里的动画
    Asset("ANIM", "anim/swap_newobrsword.zip"),                                                            -- 手里的动画
    Asset("ATLAS", "images/inventoryimages/obrsword.xml"),                                                 -- 加载物品栏贴图

    Asset("IMAGE", "images/inventoryimages/obrsword.tex"), Asset("SOUNDPACKAGE", "sound/obr_powerchange.fev"), -- 技能改动音效
    Asset("SOUND", "sound/obr_powerchange.fsb"), Asset("SOUNDPACKAGE", "sound/obrost.fev"),
    Asset("SOUND", "sound/obrost.fsb") }

local prefabs = { "sword_lunarplant_blade_fx", "hitsparks_fx", "lunarplanttentacle" }
local tools = require 'util/tools'
local skp = require 'util/obr_opskill'
local gempower = require 'util/obrsword_gempower'
--
local function SetFxOwner(inst, owner)
    if inst._fxowner ~= nil and inst._fxowner.components.colouradder ~= nil then
        inst._fxowner.components.colouradder:DetachChild(inst.blade1)
        inst._fxowner.components.colouradder:DetachChild(inst.blade2)
    end
    inst._fxowner = owner
    if owner ~= nil then
        inst.blade1.entity:SetParent(owner.entity)

        inst.blade1.Follower:FollowSymbol(owner.GUID, "swap_object", nil, nil, nil, true, nil, 0, 3)

        inst.blade1.components.highlightchild:SetOwner(owner)

        if owner.components.colouradder ~= nil then
            owner.components.colouradder:AttachChild(inst.blade1)
           
        end
    else
        inst.blade1.entity:SetParent(inst.entity)

        --For floating
        inst.blade1.Follower:FollowSymbol(inst.GUID, "swap_spear", nil, nil, nil, true, nil, 0, 3)

        inst.blade1.components.highlightchild:SetOwner(inst)
    end
end
--
local function clamp(value, min, max)
    if value < min then
        return min
    elseif value > max then
        return max
    else
        return value
    end
end

obrvoice = { "power_change/obr/obrskillbgm1", "power_change/obr/obrskillbgm2", "power_change/obr/obrskillbgm3",
    "power_change/obr/obrbgm", "power_change/obr/obrchange1", "power_change/obr/water" }

local function onsave(inst, data)
    if inst.dragonfly ~= nil then
        data.dragonfly = inst.dragonfly
        data.moose = inst.moose
        data.antlion = inst.antlion
    end
end
local function onload(inst, data)
    if data ~= nil then
        inst.dragonfly = data.dragonfly
        inst.moose = data.moose
        inst.antlion = data.antlion
    end
end

local function checkpower(inst)
    if inst.dragonfly == true and inst.moose == true and inst.antlion == true then
        return true
    end
end
local function onequip(inst, owner) -- 装备
    owner.AnimState:OverrideSymbol("swap_object", "swap_newobrsword", "swap_newobrsword")
    -- else
    --owner.AnimState:OverrideSymbol("swap_object", "swap_obrsword5", "swap_obrsword")

    -- 替换的动画部件	使用的动画	替换的文件夹（注意这里也是文件夹的名字）
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")

    if inst.components.container ~= nil then
        inst.components.container:Open(owner)
    end
    if not owner.components.obrsword_power then
        owner:AddComponent("obrsword_power")
        owner.components.obrsword_power:start_obrsword_power(owner)
    end
    local ex = owner.components.obrsword_power.extrapower + 100

    owner.components.obrsword_power:SetMax(ex)
   if owner.components.obrsword_power:Getallpower() then
    SetFxOwner(inst, owner)
   end
    --owner.components.obrsword_power:DoDelta(0)

    -- inst.maxpowertask =  inst:ListenForEvent("itemget", changepower, inst)
end

local function onunequip(inst, owner) -- 解除装备
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
    --数据联通一下
    if owner.components.obrsword_power.dragonfly == true then
        inst.dragonfly = true
    end
    if owner.components.obrsword_power.moose == true then
        inst.moose = true
    end

    if owner.components.obrsword_power.antlion == true then
        inst.antlion = true
    end

    if inst.components.container ~= nil then
        inst.components.container:Close()
    end
    SetFxOwner(inst, nil)
end
local function spawnfx(inst, fx)
    local x, y, z = inst.Transform:GetWorldPosition()
    -- SpawnPrefab(fx).Transform:SetPosition(x,-10,z)
    SpawnPrefab(fx).Transform:SetPosition(x, 4, z)
end
local function changepower(inst, data)
    skp.owner = inst.components.inventoryitem.owner
    local nop = skp.owner.components.obrsword_power
    local function addspell(inst)
        inst:AddComponent("spellcaster")
        inst.components.spellcaster.canuseontargets = true
        inst.components.spellcaster.canuseonpoint = false
        inst.components.spellcaster.canonlyuseonworkable = true
        inst.components.spellcaster.canonlyuseoncombat = true
        inst.components.spellcaster.quickcast = false
    end
    skp.normalsword(inst)
    local op = TUNING.swordtype
    if op == 0 then
        skp.normalsword(inst)
        inst.SoundEmitter:PlaySound("obr_powerchange/op/sword")
        spawnfx(inst, "obrsword_powerchange_normal")
    elseif op == 1 then
        skp.lightkinfe(inst)
        inst.SoundEmitter:PlaySound("obr_powerchange/op/sword")
        spawnfx(inst, "obrsword_powerchange_knife")
    elseif op == 2 then -- 水
        addspell(inst)
        inst.components.spellcaster:SetSpellFn(skp.swordwater)
        inst.SoundEmitter:PlaySound("obr_powerchange/op/sword")

        spawnfx(inst, "obrsword_powerchange_water")

        -- inst.components.spellcaster.castingstate = "castspell_tornado"
    elseif op == 3 and nop.dragonfly then -- 火
        addspell(inst)
        inst.components.spellcaster.quickcast = true
        inst.components.spellcaster:SetSpellFn(skp.swordfire)
        inst.SoundEmitter:PlaySound("obr_powerchange/op/fire")
        spawnfx(inst, "obrsword_powerchange_fire")
    elseif op == 4 and nop.moose then  -- 风
        addspell(inst)
        inst.components.spellcaster:SetSpellFn(skp.swordwind)
        inst.SoundEmitter:PlaySound("obr_powerchange/op/sword")
        spawnfx(inst, "obrsword_powerchange_wind")
    elseif op == 5 and nop.antlion then  -- 土
        addspell(inst)
        inst.components.spellcaster:SetSpellFn(skp.sworddirt)
        inst.SoundEmitter:PlaySound("obr_powerchange/op/sword")
        spawnfx(inst, "obrsword_powerchange_dirt")
    elseif op == 6 then  -- 光
        inst:AddComponent("spellcaster")
        inst.components.spellcaster:SetSpellFn(skp.Sword_CastLaser)
        inst.components.spellcaster.canuseonpoint = true
        inst.components.spellcaster.canuseonpoint_water = true
        inst.components.spellcaster.veryquickcast = true
        inst.SoundEmitter:PlaySound("obrost/obr/obrchange1")

        local spfx = SpawnPrefab("ghostlyelixir_fastregen_fx")
        spfx.entity:SetParent(skp.owner.entity)
    end
end
local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("obrsword") -- 地上动画
    inst.AnimState:SetBuild("obrsword")
    inst.AnimState:PlayAnimation("idle")

    inst.entity:AddSoundEmitter()

    inst:AddComponent("talker")
    inst.components.talker.fontsize = 28
    inst.components.talker.font = TALKINGFONT
    inst.components.talker.colour = Vector3(.75, .75, .75)
    inst.components.talker.offset = Vector3(0, 0, 0)
    inst.components.talker.symbol = "swap_object"
    inst:AddTag("sharp") -- 武器的标签跟攻击方式跟攻击音效有关 没有特殊的话就用这两个
    inst:AddTag("pointy")
    inst:AddTag("obrsword")
    inst:AddTag("nosteal")    --防止被火药猴偷走
    inst:AddTag("NORATCHECK") --mod兼容：永不妥协。该道具不算鼠潮分
    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    -------
    inst.blade1 = SpawnPrefab("exswapobs")
    inst.blade1.AnimState:PlayAnimation("idle", true)
    SetFxOwner(inst, nil)
    -----------
    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(TUNING.Obrsword_nadamage)
    -- inst.components.weapon:SetRange(16, 18)
    inst.components.weapon:SetOnAttack(skp.OnAttack)
    local planardamage = inst:AddComponent("planardamage")
    planardamage:SetBaseDamage(25)
    ----------------------------------
    -- inst:AddComponent("finiteuses") -- 使用次数（叫耐久也可以）
    -- inst.components.finiteuses:SetMaxUses(200)
    -- inst.components.finiteuses:SetUses(200)

    -- inst.components.finiteuses:SetOnFinished(inst.Remove) -- 没有耐久了移除武器

    inst:AddComponent("inspectable")                                                -- 可检查组件

    inst:AddComponent("inventoryitem")                                              -- 物品组件
    inst.components.inventoryitem.atlasname = "images/inventoryimages/obrsword.xml" -- 物品贴图

    inst:AddComponent("equippable")                                                 -- 可装备组件
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)

    inst:AddComponent("rechargeable")

    inst:AddComponent("container")
    inst.components.container:WidgetSetup("obrsword")
    inst.components.container.canbeopened = false

    inst.components.equippable.walkspeedmult = 1.15 -- 增加一点移速，但是到此为止不可逾越

    inst:ListenForEvent("changepower", changepower, inst)
    inst:ListenForEvent("itemget", gempower.getgempower, inst)
    inst:ListenForEvent("itemlose", gempower.losegem, inst)

    --保存数据
    inst.dragonfly = false
    inst.moose = false
    inst.antlion = false
    inst.OnSave = onsave
    inst.OnLoad = onload
    MakeHauntableLaunch(inst)

    return inst
end
local function light() -- 光的代码！
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddLight()
    inst.entity:AddNetwork()

    inst:AddTag("FX")                                     -- 特效标签

    inst.Light:Enable(true)                               -- 打开
    inst.Light:SetRadius(3)                               -- 范围
    inst.Light:SetFalloff(0.8)                            -- 削减
    inst.Light:SetIntensity(.9)                           -- 强度
    inst.Light:SetColour(180 / 255, 195 / 255, 150 / 255) -- 颜色

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end
    inst.persists = false
    return inst
end

return Prefab("obrsword", fn, assets), Prefab("sword_light", light, assets)
